package com.bawei.composeui2207a.day04

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.bawei.composeui2207a.route.RouteConfig
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 作者：赵亮
 * 时间：2024/12/13 10:55
 * 功能：导航
 */
@Composable
fun Day04_1导航() {

    // 导航控制器
    val navController = rememberNavController()
    // 启动页面
    var start = RouteConfig.START

    // 获取MMKV对象
    val mmkv = MMKV.defaultMMKV()
    val isIn = mmkv.decodeBool("isIn", false)
    if (isIn) {
        start = RouteConfig.MAIN
    } else {
        LaunchedEffect(key1 = "") {
            delay(3000)
            mmkv.encode("isIn", true)
            navController.navigate(RouteConfig.MAIN)
        }
    }

    NavHost(navController = navController, startDestination = start) {
        // 启动页面
        composable(RouteConfig.START) {
            StartPage(navController)
        }
        // 主页面
        composable(RouteConfig.MAIN) {
            MainPage(navController)
        }
    }
}

@Preview
@Composable
fun Day04_1导航Preview() {
    Day04_1导航()
}

